<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/
/** 
 * Modify a Tag
 * 
 * @package Xaraya eXtensible Management System
 * @copyright (C) 2005 by the Xaraya Development Team.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.org
 *
*/

/**
 * @author John Cox
 * @author jojodee
 * add new forum topic
 */

function conversations_user_modify_tag()
{

    if (!xarVarFetch('modify_tag', 'str:1', $tag, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('modify_tagid', 'int:0:', $tagid, '', XARVAR_DONT_SET)) return;
    
    // This should be relatively simple.  The only issue will be the returning back to
    // the page with the AJAX call.


    $timestamp = time();
    $oldtagid = $tagid;

    if (!xarSecurityCheck('Modcon', 1)) return;
    
    $data = array();
    
    // This is kinda interesting since you can also split, in addition to merge and do a merge / split all at once
    // Yuck, what have I gotten myself into???
    // We need to pull the merge out of the API and do it here.
    // Just foreach it here, and we should be OK.
    // We have to see if the tagname exist already -> merge 
    // If not, then we add the new tag, and associate the posts to the new tagid.
    // Along with all of the original attributes.
    $attributes = xarModAPIFunc('conversations', 'user', 'gettags', array(
                                              'tagid'   => $tagid));
                                              
    $subs = xarModAPIFunc('conversations', 'user', 'deletesubs', array(
                                              'tagid'   => $tagid));
                                              
    // We also need to store the post relationship before we go all bonkers...
    $posts = xarModAPIFunc('conversations', 'user', 'getlook', array(
                                                          'tagid'   => $tagid));       
    foreach ($attributes as $attribute) {
        $count = $attribute['count'];
        $user = $attribute['uid'];
        $status = $attribute['status'];
    }
    //return var_export($posts);
    
    $tags = explode(" ", $tag);
    
    foreach ($tags as $new){
        $taginfo = xarModAPIFunc('conversations', 'user', 'gettags', array(
                                                  'tag'   => $new));
                                                  
        // return var_export($taginfo);
        // There is a little bug in this when doing both a merge and a split.
        // Need to think about it for a bit.
        
        // Did the function return false?  OK, then this is not a merge
        if (empty($taginfo)){
            $new = strtolower($new);
            $newtagid = xarModAPIFunc('conversations', 'user', 'createtag', array(
                                                'tag'   => $new,
                                                'status'=> $status,
                                                'uid'   => $user,
                                                'count' => $count));
            // Tag is added, now let's also associate those original posts in here
                $totalposts = count($posts);
                for ($i = 0; $i < $totalposts; $i++) {
                    //return var_export($posts[$i]['postid']);
                    $post = $posts[$i];
                    xarModAPIFunc('conversations', 'user', 'createlook', array(
                                        'tagid'     => $newtagid,
                                        'postid'    => $posts[$i]['postid']));                
                }
                // Good???
            } else {
              
                // If there was something in taginfo, then we know that this is a merge situation
                // So lets merge 'em
                xarModAPIFunc('conversations', 'user', 'merge_tag', array(
                                                   'tag'   => $tag,
                                                   'tagid' => $tagid));  
        }
        // that should do it with this...
    }
        
    //return 'done';
    // Regardless of what happened, we need to delete the original tag...
    if (!xarModAPIFunc('conversations', 'user', 'deletetag', array('id' => $oldtagid))) return;
    
    // We don't need to play with the tag, so moving on...
    // We should just have to send it to the modify function...
    
    // Just need to return the post really...
    return $tag;
}

?>